package com.ridbor001.demo.util;


import com.zaxxer.hikari.HikariDataSource;
import lombok.Getter;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.sql.*;


public class RDBDataStore implements ServletContextListener {
    @Getter
    private static final HikariDataSource dataSource;
    static {
        dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/demo1?useSSL=false&serverTimezone=UTC");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setMaximumPoolSize(10);
        dataSource.setMinimumIdle(5);
    }


    public Connection getConnection() throws SQLException {
        return dataSource.getConnection();
}

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        if (dataSource != null) {
            dataSource.close();
        }
    }

}
